<?php

namespace App\Http\Controllers;

use Admin\Resources\ErrorResource;
use App\Repositories\Contracts\BookingInterface;
use App\Repositories\Contracts\CabinSetInterface;
use App\Repositories\Contracts\PolicyInterface;
use App\Repositories\Contracts\PolicyProductMarkInterface;
use App\Repositories\Contracts\PolicyUploadInterface;
use App\Repositories\Contracts\RuleSetInterface;
use Illuminate\Http\Request;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseResource;

class PolicyController extends BaseController
{
    private $bookingInterface;
    private $cabinSetInterface;
    private $ruleSetInterface;

    private $policyProductMarkInterface;
    private $policyUploadInterface;
    public function __construct(PolicyInterface $interface,BookingInterface $bookingInterface,
                                CabinSetInterface $cabinSetInterface,RuleSetInterface $ruleSetInterface,
                                PolicyProductMarkInterface $policyProductMarkInterface,PolicyUploadInterface $policyUploadInterface)
    {
        parent::__construct($interface);
        $this->bookingInterface = $bookingInterface;
        $this->cabinSetInterface = $cabinSetInterface;
        $this->ruleSetInterface = $ruleSetInterface;
        $this->policyProductMarkInterface = $policyProductMarkInterface;
        $this->policyUploadInterface = $policyUploadInterface;
    }

    public function add()
    {
        $bookings = $this->bookingInterface->get(['status'=>1]);
        return view('policy.add',['bookings'=>$bookings]);
    }

    public function ajaxAdd(Request $request)
    {
        //起飞日期
        $departAt =  explode(' - ',$request->depart_at)[0];
        $departEnd =  explode(' - ',$request->depart_at)[1];

        unset($request->depart_at);
        $request->merge(['depart_at'=>$departAt,'depart_end'=>$departEnd]);

        //规则有效期
        $ruleAt =  explode(' - ',$request->rule_at)[0];
        $ruleEnd =  explode(' - ',$request->rule_at)[1];

        unset($request->rule_at);
        $request->merge(['rule_at'=>$ruleAt,'rule_end'=>$ruleEnd]);

        //过滤紧张阈值
        $filterNervous = 1;
        if ($request->filter_nervous == 'off') {

            $filterNervous = 0;
        }
        unset($request->filter_nervous);
        $request->merge(['filter_nervous'=>$filterNervous]);

        //有效时间期
        $weekEnable = [];
        foreach ($request->week_enable as $key=>$value) {

            if ($value == "on") {

                $weekEnable[$key] = $key;
            }
        }
        $request->merge(['week_enable'=>implode(',',$weekEnable)]);


        $data = $this->getModelField($request);

        $res = $this->interface->add($data);

        if ($res) {

            $this->cabinSetInterface->create($request->all(),$res->id);
            $this->ruleSetInterface->create($request->all(),$res->id);
            $this->policyProductMarkInterface->create($request->all(),$res->id);
            return new BaseResource($res);
        }
        return new ErrorResource([]);
    }


    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View|BaseResource|\MakeRep\Resources\ErrorResource
     */
    public function update(Request $request)
    {
        $bookings = $this->bookingInterface->get(['status'=>1]);
        $res = $this->interface->findById($request->id);

        $productMark = [];
        if ($res->product_mark){

            $productMark = $res->product_mark->pluck('product_mark')->toArray();
        }

        return view('policy.update',['bookings'=>$bookings,'policy'=>$res,'product_mark'=>$productMark]);
    }

    /**
     * 更新
     * @param Request $request
     * @return mixed
     */
    public function ajaxUpdate(Request $request){

        //起飞日期
        $departAt =  explode(' - ',$request->depart_at)[0];
        $departEnd =  explode(' - ',$request->depart_at)[1];

        unset($request->depart_at);
        $request->merge(['depart_at'=>$departAt,'depart_end'=>$departEnd]);

        //规则有效期
        $ruleAt =  explode(' - ',$request->rule_at)[0];
        $ruleEnd =  explode(' - ',$request->rule_at)[1];

        unset($request->rule_at);
        $request->merge(['rule_at'=>$ruleAt,'rule_end'=>$ruleEnd]);

        //过滤紧张阈值
        if ($request->filter_nervous == 'on') {

            $filterNervous = 1;
        } else {
            $filterNervous = 0;
        }
        unset($request->filter_nervous);
        $request->merge(['filter_nervous'=>$filterNervous]);

        //有效时间期
        $weekEnable = [];
        foreach ($request->week_enable as $key=>$value) {

            if ($value == "on") {

                $weekEnable[$key] = $key;
            }
        }
        $request->merge(['week_enable'=>implode(',',$weekEnable)]);


        $data = $this->getModelField($request);

        $res = $this->interface->update(['id'=>$request->id],$data);

        if ($res) {

            if ($request->status == 2) {

                $this->policyUploadInterface->delete([['policy_code','like',$request->policy_code.'%'],['platform','=',$request->product_type]]);
            }

            $this->cabinSetInterface->create($request->all(),$request->id);
            $this->ruleSetInterface->create($request->all(),$request->id);
            $this->policyProductMarkInterface->create($request->all(),$request->id);
            return new BaseResource($res);
        }
        return new ErrorResource([]);
    }


    /**
     * 单项更新
     * @param Request $request
     * @return mixed
     */
    public function itemUpdate(Request $request){

        $data = [];
        if ($request->status) {

            $data['status'] = $request->status;
        }

        $res = $this->interface->update(['id'=>$request->id],$data);
        if ($res) {

            if ($request->status == 2) {

                $res = $this->policyUploadInterface->delete([['policy_code','like',$request->policy_code.'%'],['platform','=',$request->product_type]]);
                if ($res !== false) {
                    return new BaseResource([]);
                } else {
                    return new ErrorResource([]);
                }
            }
            return new BaseResource([]);
        }

        return new ErrorResource([]);
    }
}